package com.ckerchina.admin.ckadmin.service.mgr;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint;

public class AjaxLoginUrlAuthenticationEntryPoint extends LoginUrlAuthenticationEntryPoint {

	public AjaxLoginUrlAuthenticationEntryPoint(String loginFormUrl) { 
		super(loginFormUrl); 
	} 
 
	@Override
	public void commence(HttpServletRequest request, HttpServletResponse response,
			AuthenticationException authException) throws IOException, ServletException {
		String XRequestedWith = request.getHeader("X-Requested-With");
		if ("XMLHttpRequest".equals(XRequestedWith) || "Ext.basex".equals(XRequestedWith)) {
			response.sendError(HttpServletResponse.SC_FORBIDDEN, "Access Denied");// 对于ajax请求不重定向 而是返回错误代码
		} else {
			super.commence(request, response, authException); 
		}
	}

}
